package com.ruoyi.web.controller.follow;

import java.util.List;

import com.ruoyi.follow.domain.vo.PanelData;
import com.ruoyi.follow.domain.vo.UserFollowVO;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.follow.domain.UserFollow;
import com.ruoyi.follow.service.IUserFollowService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

/**
 * 用户关注Controller
 * 
 * @author chen
 * @date 2025-07-04
 */
@RestController
@RequestMapping("/follow/follow")
@Tag(name = "【用户关注】管理")
public class UserFollowController extends BaseController
{
    @Autowired
    private IUserFollowService userFollowService;

    /**
     * 查询用户关注列表
     */
    @Operation(summary = "查询用户关注列表")
    @PreAuthorize("@ss.hasPermi('follow:follow:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserFollow userFollow)
    {
        startPage();
        List<UserFollow> list = userFollowService.selectUserFollowList(userFollow);
        return getDataTable(list);
    }

    /**
     * 导出用户关注列表
     */
    @Operation(summary = "导出用户关注列表")
    @PreAuthorize("@ss.hasPermi('follow:follow:export')")
    @Log(title = "用户关注", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserFollow userFollow)
    {
        List<UserFollow> list = userFollowService.selectUserFollowList(userFollow);
        ExcelUtil<UserFollow> util = new ExcelUtil<UserFollow>(UserFollow.class);
        util.exportExcel(response, list, "用户关注数据");
    }

    /**
     * 获取用户关注详细信息
     */
    @Operation(summary = "获取用户关注详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userFollowService.selectUserFollowById(id));
    }

    /**
     * 新增用户关注
     */
    @Operation(summary = "新增用户关注")
    @Log(title = "用户关注", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserFollow userFollow)
    {
        return toAjax(userFollowService.insertUserFollow(userFollow));
    }

    /**
     * 根据UserFollow删除用户关注
     */
    @Operation(summary = "根据UserFollow删除用户关注")
    @Log(title = "用户关注", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete")
    public AjaxResult deleteByUserFollow(@RequestBody UserFollow userFollow)
    {
        return toAjax(userFollowService.deleteByUserFollow(userFollow));
    }

    /**
     * 删除用户关注
     */
    @Operation(summary = "删除用户关注")
    @Log(title = "用户关注", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable( name = "id" ) Long id)
    {
        return toAjax(userFollowService.deleteUserFollowById(id));
    }

    /**
     * 根据粉丝id获取粉丝的关注列表
     */
    @Operation(summary = "根据粉丝id获取粉丝的关注列表")
    @GetMapping(value = "/fans/{fansId}")
    public AjaxResult getFollowedByFansId(@PathVariable("fansId") Long fansId)
    {
        List<UserFollowVO> list = userFollowService.getFollowedByFansId(fansId);
        return success(list);
    }

    /**
     * 根据被关注用户id获取粉丝列表
     */
    @Operation(summary = "根据被关注用户id获取粉丝列表")
    @GetMapping(value = "/followed/{followedUserId}")
    public AjaxResult getFansByFollowedUserId(@PathVariable("followedUserId") Long followedUserId)
    {
        List<UserFollowVO> list = userFollowService.getFansByFollowedUserId(followedUserId);
        return success(list);
    }

    /**
     * 获取用户关注数、粉丝数、帖子点赞总数
     */
    @Operation(summary = "根据被关注用户id获取粉丝列表")
    @GetMapping(value = "/panel/{userId}")
    public AjaxResult getPanelDataForUser(@PathVariable("userId") Long userId)
    {
        List<PanelData> list = userFollowService.getPanelDataForUser(userId);
        return success(list);
    }

}
